<?php

class ProfileController extends Controller {
    protected $allowed = array('administrator' , 'member');

    public function __construct() {
        parent::__construct();
    }

    public function ajax_load_photos($user_id) {
        $user = Model::factory('auth', $user_id);
        if ($user->loaded()) {
            $photos = $user->photo;
            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $photos = Model::paginaze($photos, $page , 3);

            foreach($photos as $photo) { ?>
            <div class="photoThumb">
                <a href="<?php echo $this->createUrl('photo/detail/'. $photo->id);?>">
                    <img src="<?php echo $photo->getUrl();?>" alt="" />
                </a>
            </div>
            <?php
            }
        }
    }

    public function view($user_id) {
        $user = Model::factory('auth', $user_id);
        if ($user->loaded()) {
            $walls = Model::loadRowsFromQuery("SELECT * FROM wall WHERE user_id = ".$user->id." ORDER BY id DESC LIMIT 1;" , "WallModel");
            $photos = $user->photo;

            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $pagin = new Pagination(count($photos) , $this->createUrl('profile/ajax_load_photos/' . $user->id) , 3);
            $photos = Model::paginaze($photos, $page , 3);

            $this->render("profile/view" , array('user' => $user , 'walls' => $walls , 'photos' => $photos , 'pagin' => $pagin));
        }
    }

    public function getAllWall($user_id) {
        $user = Model::factory('auth', $user_id);
        if ($user->loaded()) {
            $walls = Model::loadRowsFromQuery("SELECT * FROM wall WHERE user_id = ".$user->id." ORDER BY id DESC LIMIT ".($_GET['page'] * 1).";" , "WallModel");
            $this->render("profile/ajaxview" , array('user' => $user , 'walls' => $walls));
        }
    }

    public function edit() {
        if ($this->isAllowed()) {
            if ($_POST) {
                $user = Model::factory('auth' , $this->auth->id);
                //print_r($user);
                $user->fullname = $_POST['name'];
                $user->setPassword($_POST['password']);
                $user->birthday = $_POST['bday'];
                $user->email = $_POST['email'];
                $user->sex = $_POST['sex'];
                $user->location = $_POST['location'];
                $user->profile_rule = $_POST['profile_rule'];
                $user->save();
                $this->redirect($this->createUrl('profile/edit'));
            }
            else
                $this->render('profile/edit');
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function message($user_id) {
        if ($this->isAllowed()) {
            $user = Model::factory('auth' , $user_id);
            if ($user->loaded()) {
                if ($_POST) {
                    $message = Model::factory('message');
                    $message->subject = $_POST['subject'];
                    $message->body = $_POST['body'];
                    $message->sender_id = $this->auth->id;
                    $message->recipient_id = $user_id;
                    $message->status = MessageModel::$STATUS_UNREAD;
                    $message->save();
                    $this->redirect($this->createUrl('profile/view/' . $user_id));
                }
                else {
                    $this->render('profile/message' , array('user' => $user));
                }
            }
            else
                $this->showError('No user exists');
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function commentwall($wall_id) {
        if ($this->isAllowed()) {
            $wall = Model::factory('wall' , $wall_id);
            if ($wall->loaded()) {
                $newWall = Model::factory('wall');
                $newWall->content = $_POST['commentwall'];
                $newWall->poster_id = $this->auth->id;
                $newWall->wall_id = $wall->id;
                $newWall->save();

                $this->redirect($this->createUrl('profile/view/' . $wall->auth->id));
            }
            else
                $this->showError('Wall does not exist');
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function postwall($user_id) {
        if ($this->isAllowed()) {
            $user = Model::factory('auth' , $user_id);
            /*if($user->isWallAllowed($this->auth))
                echo 'allowed';
            else
                echo 'not allowed';*/
            
            if ($user->loaded() && $_POST && $user->isWallAllowed($this->auth)) {
                $wall = new WallModel();
                $wall->content = $_POST['wall'];
                $wall->user_id = $user->id;
                $wall->poster_id = $this->auth->id;
                $wall->save();
                $this->redirect($this->createUrl('profile/view/' . $user_id));
            }
            else
                $this->showError('No user exists');
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function follow($user_id) {
        $user = Model::factory('auth' , $user_id);
        if ($this->isAllowed() && $user->loaded()) {
            $this->auth->follow($user_id);
            
            //Add 'notification' table
            $newNotif = Model::factory('notification');
            $newNotif->content  = '<a href="'.$this->createUrl('profile/view/'.$this->auth->id).'">'.$this->auth->fullname.'</a> has followed you';
            $newNotif->user_id  = $user_id;
            $newNotif->status   = NotificationModel::$NOTIF_UNREAD;
            $newNotif->save();

            $this->redirect($this->createUrl('profile/view/' . $user_id));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function unfollow($user_id) {
        $user = Model::factory('auth' , $user_id);
        if ($this->isAllowed() && $user->loaded()) {
            $this->auth->unfollow($user_id);
            $this->redirect($this->createUrl('home/index'));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

}

?>
